import asyncio
import json

from com.arcfox.base.base_spider import BaseSpider, ExecuteType
from com.arcfox.school.processor.diebian.diebian_pheasant_college_processor import DieBianPheasantCollegeProcessor
from com.arcfox.util import async_request as requests
from com.arcfox.middleware.async_redis_middleware import open_redis


class DieBianPheasantCollegeSpider(BaseSpider):
    '''
    野鸡大学抓取
    '''
    def __init__(self):
        self.url = "https://apiv3.diebian.net/College/v1/FakeSchool"
        self.processor = DieBianPheasantCollegeProcessor()

    @open_redis
    async def init_data_version(self, client):
        return await super().init_data_version(client)

    async def _pull_task(self):
        return True, ExecuteType.ONCE

    async def _crawl_by_task(self, task):
        headers = self.get_default_header()
        headers.update({'Content-Type': 'application/json'})
        params = {
            "pageNum": 1,
            "pageSize": 1000,
            "name": ""
        }
        resp = await requests.post_with_session(url=self.url, headers=headers, data=json.dumps(params))
        if resp.code == 200:
            await self.processor.parse_and_save_data(resp.json())


if __name__ == "__main__":
    asyncio.run(DieBianPheasantCollegeSpider().start_crawl())
